<?php
$user = $data['user'];
//dump($data);

function productType($type) {
    $arr = ['图片', '附件', '外部链接'];
    return $arr[$type];
    //return $type;
}

?>
<!DOCTYPE html>
<html lang="zhcn">
<head>
    <include file="Common/meta" />
    <title>我的作品 - {$_ENV['site'][0]['title']}</title>
    <include file="Common/link" />
</head>
<body>
<header class="layout-header">
    <div class="container">
        <h1 class="title">作品集</h1>
        <a class="header-back" href="/user/personCenter"><i class="icon icon-left"></i></a>
        <ul class="tool-list">
            <li class="tool-item"><a id="add" href="#">添加</a></li>
        </ul>
    </div>
</header>
<div class="layout-body">
    <div class="container">
        <if condition="count($data['list']) eq 0">
            <div id="search-empty" class="content-empty show">
                <div class="picture"></div>
                <div class="no-data">您还没有添加作品</div>
            </div>
        </if>
        <ul id="work-list" class="list work-list">
            <foreach name="data['list']" item="product">
                <li class="list-item product-item" data-id="{$product['product_id']}" data-name="{$product['product_name']}" data-type="{$product['product_type']}" data-url="{$product['product_file']}">
                    <div id="info" class="info-box">
                        <div class="info">{$product['product_name']}</div>
                        <div class="info">类型：
                            {$product['product_type']|productType}
                        </div>
                        <div class="info">
                            <empty name="product['product_file']">
                                没有附件
                                <else />
                                <if condition="$product['product_type'] eq 1">
                                    <a class="btn btn-primary" href="{$product['product_file']}" >下载附件</a>
                                    <elseif condition="$product['product_type'] eq 0" />
                                    <a class="btn btn-primary show-product-btn" href="{$product['product_file']}" >查看图片</a>
                                    <else />
                                    <a class="btn btn-primary show-product-btn" href="{$product['product_file']}" >打开链接</a>
                                </if>

                            </empty>

                        </div>
                        <a class="edit btn show-product-btn" data-id="{$product['product_id']}" href="#"><i class="icon icon-edit"></i> 编辑</a>
                        <i class="icon icon-close"></i>
                    </div>

                </li>

            </foreach>
        </ul>
    </div>
</div>

<!-- 视频链接弹出框 -->
<div id="like-wrap" class="like-wrap">
    <section class="box box-like">
        <div class="box-body">
            <p> </p>
            <textarea id="link-text" placeholder="请输入优酷视频网址" rows="4"></textarea>
            <input id="link-btn" class="login-btn" type="button" value="确定">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->
<!-- 查看图片弹出框 -->
<div id="lightbox" class="lightbox">
    <section class="box box-like">
        <div class="box-body">
            <p> </p>
            <img id="lightbox-img" class="lightbox-img" src="/Uploads/2016-08-28/57c24893a6f22.jpg">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->
<!-- 添加作品弹出框 -->
<div id="work-wrap" class="like-wrap">
    <section class="box box-like">
        <div class="box-body">
            <div class="tc-logo-box">
                <img class="logo" src="/public/asset/img/logo_blue.png">
                <div class="text">编辑作品</div>
            </div>
            <input id="work-dialog-name" class="input" placeholder="作品名称">
            <p>选择作品类型：</p>
            <select id="work-dialog-type" class="input">
                <option value="1">附件</option>
                <option value="2">外部链接</option>
            </select>
            <input id="work-dialog-file" type="file">
            <p id="work-dialog-tip" class="tip">附件仅支持jpg、png、pdf、ppt和doc格式</p>
            <input id="work-dialog-link" class="input tc-hide" placeholder="外部链接">
            <input id="work-dialog-add" class="login-btn" type="button" value="保存">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->

<include file="Common/script" />
<script>
    /*var page = {
        isOpen: {$user['is_open']}
    }*/
</script>
<script>
    $('.show-product-btn').on('click',function(){
        eui.alert('请用电脑访问tswcapp.com进行编辑');
    });
    // 删除作品
    $('#work-list').on('click', '.icon-close', function (e) {
        e.preventDefault();
        var $this = $(this);
        var $item = $this.closest('.product-item');
        var productId = $item.data('id');

        $.ajax({
            url: '/stdproduct/del',
            data: {
                product_id: productId
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('删除成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 800);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        });
    });

    var isAdd = true;
    // 添加作品
    $('#add').on('click', function (e) {

        eui.alert('请用电脑访问 tswcapp.com添加');
        // isAdd = true;

        // $('#work-dialog-type').val(1);
        // $('#work-dialog-name').val('');
        // $('#work-dialog-link').hide();
        // $('#work-dialog-link').val('');
        // $('#work-dialog-file').show();
        // $('#work-dialog-file').val('')
        // $('#work-dialog-tip').show();

        // $('#work-wrap').dialog();
    });

    // 切换作品类型
    $('#work-dialog-type').on('change', function () {
        if ($(this).val() == 1) { // 附件
            $('#work-dialog-link').hide();
            $('#work-dialog-file').show();
            $('#work-dialog-tip').show();
        }  else {
            $('#work-dialog-link').show();
            $('#work-dialog-file').hide();
            $('#work-dialog-tip').hide();
        }
    })
    // 编辑作品保存
    $('#work-dialog-add').on('click', function (e) {
        e.preventDefault();

        var type = $('#work-dialog-type').val();
        var name = $('#work-dialog-name').val();
        if (!name) {
            eui.msg('请输入作品名称');
            return;
        }

        var formData = new FormData();

        if (type == 0 || type == 1) { // 附件

            var files = $('#work-dialog-file')[0].files;
            if (isAdd && (!files || !files.length)) {
                eui.msg('请上传附件');
                return;
            }

            var fileName = $('#work-dialog-file').val();
            var dot = fileName.lastIndexOf('.');
            var type = fileName.toLowerCase().substring(dot + 1);
            if ('jpg|png|pdf|ppt|pptx|doc|docx|pptx'.indexOf(type) == -1) {
                eui.msg('附件格式不正确');
                return;
            }

            if (files[0].size > 2 * 1024 * 1024) {
                eui.msg('附件不能大于2M');
                return;
            }

            formData.append('file', files[0]);

        } else {
            var link = $('#work-dialog-link').val();
            if (!link) {
                eui.msg('请输入外部链接');
                return;
            }

            function isUrl(url){
                var exp = '^https?://([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,8}'
                        + '(/([0-9a-zA-Z-_\.]+)*?)*' // 文件
                        + '[\\d\\D]*'
                        //+ '(#[0-9a-zA-Z-_]??)?' // hash
                        + '$';
                var reg = new RegExp(exp);
                return reg.test(url);
            }

            if (!isUrl(link)) {
                eui.msg('链接格式不正确');
                return;
            }

            formData.append('file', link);
        }

        formData.append('product_name', name);

        var url;
        if (isAdd) {
            url = '/stdproduct/add';
        } else {
            url = '/stdproduct/update';
            formData.append('product_id', $operateItem.data('id'));
        }

        $.ajax({
            url: url,
            data: formData,
            type: 'POST',
            dataType: 'json',
            processData: false,
            contentType: false,
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('保存成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 800);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });



    var $operateItem;

    // 查看图片附件
    $(document).on('click', '.lightbox-show', function (e) {
        e.preventDefault();

        $('#lightbox-img').attr('src', $(this).attr('href'));
        $('#lightbox').dialog();
    });

    // 设置是否对外显示重要信息
    $('#modify-open').on('click', function (e) {
        e.preventDefault();

        $.ajax({
            url: '/user/hideInfoToggle',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    //location.reload(true);
                    if (page.isOpen === 1) {
                        page.isOpen = 0;
                        $('#open-state').text('否');
                    } else {
                        page.isOpen = 1;
                        $('#open-state').text('是');
                    }
                } else {
                    eui.msg('操作失败');
                }
            },
            error: function () {
                eui.msg('系统出错');
            }
        });
    });


</script>
</body>
</html>

